package com.team.abs.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
@Service
public class kmeanstitleServiceImp implements kmeanstitleService {

	@Override
	public List<String> title(String filename, int k) {
		
		String a="D:\\textfile\\"+filename+"_jieba.txt";
		String b=String.valueOf(k);
		//System.out.println(a);
		ArrayList<String>list=new ArrayList();
		try {
			String[]args=new String[]{ "python", "D:\\pythonplace\\spider\\weibo\\do_kmeans.py",a,b};
			   Process proc = Runtime.getRuntime().exec(args);// 执行py文件
				 
			    //BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
			    //这里输出流出了问题 之前都没问题 直接转GBK了
			   BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), Charset.forName("GBK")));
			    String line = null;
			   
			    while ((line = in.readLine()) != null) {
			    	//这里用原来的转UTF-8就乱码了
			    	//String st=new String(line.getBytes("GB2312"), "UTF-8");
			    	line=line.replaceAll("  ", " ");
			    	list.add(line);
			    	//System.out.println(line);
			    }
			    in.close();
			    proc.waitFor();
			} catch (IOException e) {
			    e.printStackTrace();
			} catch (InterruptedException e) {
			    e.printStackTrace();
			}
				return list;
	}

}
